// packageStudent/pages/user/job-intention/index.js
import {getDictList} from "../../../service/Common";
import {add, edit, getList} from "../../../service/JobIntention";
import Notify from "@vant/weapp/notify/notify";
import Toast from "@vant/weapp/toast/toast";
import Config from "../../../main.config";
import {getStorageSync} from "../../../utils/Storage";
Page({

    /**
     * 页面的初始数据
     */
    data: {
        nature: {
            title: "岗位性质",
            value: "",
            list: [],
            current: -1
        },
        industryNo: "",
        industryName: "",
        professionalName: "",
        city: "",
        salary: {
            title: "薪资要求",
            value: "",
            list: [],
            current: -1
        },

        /*--组件数据--*/
        picker: { //选择器
            show: false,
            loading: false,
            name: "", //选择器名称
            title: "",  //选择器标题
            list: [],
            defaultIndex: 0
        },
        region: { //区域选择器
            show: false,
            value: ""
        },
        isLoading: false // 按钮状态
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.todo === "create") {return false};
        this.data._userId = getStorageSync(Config.storage.userInfo + ".userId");
        this.getJobList();
    },

    /*
    * 获取受教育列表
    * */
    getJobList: async function () {
        try {
            let userId = this.data._userId;
            let list = await await getList(userId);
            let detail = list.length > 0 ? list[0] : null;
            this.data._jobIntentionDetail = detail;
            detail ? this.setData({
                "nature.value": detail.nature,
                industryNo: detail.industryNo,
                industryName: detail.industryName,
                professionalName: detail.professionalName,
                city: detail.city.split(",")[1],
                "salary.value": detail.salary,
                "region.value": detail.city
            }) : null
        } catch (e) {
            console.error(e)
        }
    },

    /*
    * 选择器展开
    * */
    bindShowPicker: async function (e) {
        try {
            const that = this;
            let name = e.currentTarget.dataset.name;
            let title = this.data[name].title;
            let list = this.data[name].list;
            let current = this.data[name].current;
            this.setData({"picker.show": true});
            if (list && list.length === 0) { //没有数据的情况下
                this.setData({"picker.loading": true});
                switch (name) {
                    case "nature": //岗位性质
                        list = await getDictList("GWXZ");
                        break
                    case "salary": //企业行业
                        list = await getDictList("XZFW");
                        break
                }
                this.data[name].list = list;
            }
            this.setData({
                "picker.loading": false,
                "picker.name": name,
                "picker.title": title,
                "picker.list": list
            }, function () {
                that.setData({"picker.defaultIndex": current})
            })
        } catch (e) {
            console.error(e)
        }
    },
    // 选择器选择
    onPickerConfirm: function (e) {
        let name = e.currentTarget.dataset.name;
        let detail = e.detail;
        this.setData({
            "picker.show": false,
            [name + ".value"]: detail.value.text
        })
        this.data[name].id = detail.value.value;
        this.data[name].current = detail.index;
    },
    // 选择器关闭
    onPickerCancel: function (e) {
        this.setData({"picker.show": false})
    },

    /*
    * 跳转查询页面
    * */
    bindToSelected: function (e) {
        const that = this;
        let name = e.currentTarget.dataset.name;
        if (name === "industryName") {
            wx.navigateTo({
                url: "/pages/common/industry-search/index",
                events: {
                    acceptDataFromOpenedPage: function(data) {
                        console.log(data);
                        that.data.industryNo = data.industryNo;
                        that.setData({industryName: data.industryName})
                    }
                },
            })

        } else {
            wx.navigateTo({
                url: "/pages/common/position-search/index",
                events: {
                    acceptDataFromOpenedPage: function(data) {
                        console.log(data);
                        that.setData({
                            professionalName: data.positionName
                        })
                    }
                },
            })
        }
    },

    /*
    * 获取定位信息
    * */
    getLocationRegion: function () {
        this.setData({"region.loading": true});
    },
    onLocation: function (e) {
        let detail = e.detail;
        if (Object.keys(detail).length > 0) {
            this.setData({
                "region.value": detail.region,
                city: detail.region.split(",")[1],
                addressDetail: detail.address
            })
        } else {
            this.setData({"region.show": true})
        }
    },
    // 区域选择
    onSelectRegion: function (e) {
        let detail = e.detail;
        this.setData({"region.show": false});
        if (!detail) return false;
        this.setData({
            "region.value": detail,
            city: detail.split(",")[1]
        })
    },

    /*
    * 提交保存
    * */
    onSubmitButton: async function () {
        try {
            let nature = this.data.nature.value;
            let industryNo = this.data.industryNo;
            let industryName = this.data.industryName;
            let professionalName = this.data.professionalName;
            let city = this.data.region.value;
            let salary = this.data.salary.value;
            let isLoading = this.data.isLoading;
            if (isLoading) { //防重复
                return false
            } else if (!nature) {
                Notify("请选择岗位性质");
                return false
            } else if (!industryName) {
                Notify("请填期望行业");
                return false
            } else if (!professionalName) {
                Notify("请选择期望职位");
                return false
            } else if (!city) {
                Notify("请选择工作城市");
                return false
            } else if (!salary) {
                Notify("请选择期望薪资");
                return false
            } else {
                let jobIntentionDetail = this.data._jobIntentionDetail;
                let jobId = jobIntentionDetail ? jobIntentionDetail.id : null;
                let postData = {
                    nature,
                    industryNo,
                    industryName,
                    professionalName,
                    city,
                    salary
                }
                this.setData({isLoading: true});
                if (jobId) { //编辑
                    postData.id = jobId;
                    await edit(postData);
                    Toast.success("保存成功");
                    setTimeout(function () {
                        wx.navigateBack()
                    }, 1500)
                } else {
                    await add(postData);
                    Toast.success("保存成功");
                    setTimeout(function () {
                        wx.redirectTo({
                            url: "/pages/index/index"
                        })
                    }, 1500)
                }
            }
        } catch (e) {
            console.error(e)
        } finally {
            this.setData({isLoading: false});
        }
    }
})